package com.backend.controller;

import com.backend.entity.Result;
import com.backend.entity.ResultCode;
import com.backend.entity.entity.Devices;
import com.backend.entity.entity.Users;
import com.backend.entity.entity.WorkOrders;
import com.backend.service.AdminService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 管理员模块
 */
@RestController
@RequestMapping("/admin")
@Tag(name = "管理员模块接口")
public class AdminController {

    @Autowired
    private AdminService adminService;

    /**
     * 获取所有用户信息
     * @return 用户列表，包含用户名和角色信息
     */
    @GetMapping("/users")
    @Operation(summary = "获取所有用户信息")
    public Result getAllUsers() {
            List<Users> users = adminService.getAllUsers();
            return Result.success(users);
    }
    /**
     * 根据用户名删除用户
     * @param username 用户名
     * @return 操作结果
     */
    @DeleteMapping("/users")
    @Operation(summary = "根据用户名删除用户")
    public Result deleteUserByUsername(@RequestParam String username) {
            boolean success = adminService.deleteUserByUsername(username);
            if (success) {
                return Result.success("删除用户成功");
            } else {
                return Result.failure(ResultCode.FAILURE, "用户不存在");
            }
    }

    /**
     * 根据用户名查找用户
     * @param username 用户名
     * @return 用户信息，包含用户名和角色
     */
    @GetMapping("/users/search")
    @Operation(summary = "根据用户名查找用户")
    public Result getUserByUsername(@RequestParam String username) {
            List<Users> user = adminService.getUserByUsername(username);
            System.out.println(user);
            if (user != null) {
                return Result.success(user);
            } else {
                return Result.failure(ResultCode.FAILURE, "用户不存在");
            }
    }

    /**
     * 获取所有维修订单
     * @return 订单列表，包含订单号、用户ID、维修工ID、设备编号、问题描述、创建时间和状态
     */
    @GetMapping("/orders")
    @Operation(summary = "获取所有维修订单")
    public Result getAllOrders() {
            List<WorkOrders> orders = adminService.getAllOrders();
            return Result.success(orders);
    }

    /**
     * 根据订单号删除维修订单
     * @param orderId 订单号
     * @return 操作结果
     */
    @DeleteMapping("/orders")
    @Operation(summary = "根据订单号删除维修订单")
    public Result deleteOrderById(@RequestParam Integer orderId) {

            boolean success = adminService.deleteOrderById(orderId);
            if (success) {
                return Result.success("删除订单成功");
            } else {
                return Result.failure(ResultCode.FAILURE, "订单不存在");
            }

        }

    /**
     * 根据设备编号查找维修订单
     * @param deviceCode 设备编号
     * @return 订单列表，包含订单号、用户ID、维修工ID、设备编号、问题描述、创建时间和状态
     */
    @GetMapping("/orders/search")
    @Operation(summary = "根据设备编号查找维修订单")
    public Result getOrdersByDeviceCode(@RequestParam String deviceCode) {
            List<WorkOrders> orders = adminService.getOrdersByDeviceCode(deviceCode);
            return Result.success(orders);
    }

    /**
     * 更新维修订单状态
     * @param orderId 订单号
     * @param status 订单状态：1=维修中，2=已完成，3=维修失败
     * @return 操作结果
     */
    @PutMapping("/orders/status")
    @Operation(summary = "更新维修订单状态")
    public Result updateOrderStatus(
            @RequestParam Integer orderId,
            @RequestParam Integer status,
            @RequestParam(required = false) String deviceCode,
            @RequestParam(required = false) Integer repairmanId) {         
            if (status < 1 || status > 3) {
                return Result.failure(ResultCode.FAILURE, "无效的状态值，状态必须为1、2或3");
            }
            boolean success = adminService.updateOrderStatus(orderId, status, deviceCode, repairmanId);
            if (success) {
                return Result.success("更新订单状态成功");
            } else {
                return Result.failure(ResultCode.FAILURE, "订单不存在");
            }
    }

    /**
     * 添加新设备
     * @param device 设备信息
     * @return 操作结果
     */
    @PostMapping("/devices")
    @Operation(summary = "添加新设备")
    public Result addDevice(@RequestBody Devices device) {
            // 验证设备信息
            if (device.getDeviceCode() == null || device.getDeviceCode().isEmpty()) {
                return Result.failure(ResultCode.FAILURE, "设备编号不能为空");
            }
            if (device.getModel() == null || device.getModel().isEmpty()) {
                return Result.failure(ResultCode.FAILURE, "设备型号不能为空");
            }
            // 默认为正常状态
            if (device.getStatus() == null) {
                device.setStatus(0);
            }
            Devices newDevice = adminService.addDevice(device);
            return Result.success(newDevice);
    }

    /**
     * 获取所有设备信息
     * @return 设备列表
     */
    @GetMapping("/devices")
    @Operation(summary = "获取所有设备信息")
    public Result getAllDevices() {
            List<Devices> devices = adminService.getAllDevices();
            return Result.success(devices);
    }

    /**
     * 根据设备ID删除设备
     * @param deviceId 设备ID
     * @return 操作结果
     */
    @DeleteMapping("/devices")
    @Operation(summary = "根据设备ID删除设备")
    public Result deleteDeviceById(@RequestParam Integer deviceId) {
            boolean success = adminService.deleteDeviceById(deviceId);
            if (success) {
                return Result.success("删除设备成功");
            } else {
                return Result.failure(ResultCode.FAILURE, "设备不存在");
            }
    }

    /**
     * 更新设备信息
     * @param device 设备信息
     * @return 操作结果
     */
    @PutMapping("/devices")
    @Operation(summary = "更新设备信息")
    public Result updateDevice(@RequestBody Devices device) {
            if (device.getDeviceId() == null) {
                return Result.failure(ResultCode.FAILURE, "设备ID不能为空");
            }
            Devices updatedDevice = adminService.updateDevice(device);
            if (updatedDevice != null) {
                return Result.success(updatedDevice);
            } else {
                return Result.failure(ResultCode.FAILURE, "设备不存在");
            }
    }
    @GetMapping("/devices/search")
    @Operation(summary = "根据设备编号查找设备")
    public Result getDeviceByCode(@RequestParam String deviceCode) {
        Devices device = adminService.getDeviceByCode(deviceCode);
        if (device != null) {
            return Result.success(device);
        } else {
            return Result.failure(ResultCode.FAILURE, "设备不存在");
        }
    }
}
